<template>
  <ContentWrap>
    <template #header>
      <div class="card-header">
        <el-radio-group v-model="activeName" @change="handleClick">
          <el-radio-button label="待办" value="1" />
          <el-radio-button label="已办" value="2" />
        </el-radio-group>
        <span class="more" @click="routeToMore">更多></span>
      </div>
    </template>
    <div class="card-content">
      <div class="list-item" v-for="(item, index) in dataList" :key="index" @click="handleAudit(item)">
        <div class="list-item-title ellipsis ellipsis-1">
          <span :title="dataStr(item)">{{ dataStr(item) }}</span>
        </div>
        <div class="list-item-date">
          {{ formatDate(item.createTime, 'YYYY-MM-DD') }}
        </div>
      </div>
    </div>
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as TaskApi from '@/api/bpm/task'
import { formatDate } from '@/utils/formatTime'
const { push } = useRouter() // 路由
const activeName = ref('1')
const query = reactive({
  pageNo: 1,
  pageSize: 5,
})
const dataStr = computed(() => {
  return (item: any) => {
    if(activeName.value === '1') {
      return `${item.processInstance?.startUser?.nickname}创建了${item.processInstance?.name}，待审核。`
    } else {
      return `${item.processInstance?.startUser?.nickname}完成了${item.processInstance?.name}。`
    }
  }
})
const dataList = ref<any>([])
const getList = () => {
  const fn = activeName.value === '1' ? TaskApi.getTaskTodoPage : TaskApi.getTaskDonePage
  fn({...query}).then(res => {
    dataList.value = res.list})
}
const handleClick = () => {
  getList()
}
/** 处理审批按钮 */
const handleAudit = (row: any) => {
  push({
    name: 'BpmProcessInstanceDetail',
    query: {
      id: row.processInstance.id,
      taskId: row.id
    }
  })
}
/** 更多 */
const routeToMore = () => {
  if(activeName.value === '1') {
    push({name: 'BpmTodoTask'})
  } else {
    push({name: 'BpmDoneTask'})
  }
}
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.card-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .card-title {
    font-weight: bold;
    line-height: 32px;
  }
  .more {
    font-size: 14px;
    cursor: pointer;
  }
  :deep(.el-tabs__header) {
    margin: 0;
  }
}
.card-content {
  padding: 0 10px;
  height: 216px;
}
.list-item {
  display: flex;
  padding: 10px 0;
  border-bottom: 1px solid var(--el-card-border-color);
  cursor: pointer;
  &:last-child {
    border-bottom: 0;
  }
  .list-item-title {
    width: 0;
    flex: 1;
  }
}
:deep(.el-card__header) {
  padding: 10px;
}
</style>
